package com.levi.utils.log.printer;

import android.util.Log;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Locale;

/**
 * Created by Levi on 2018/3/13.
 */
public class LogFilePrinter extends Printer {

    private static final String TAG = "LogFilePrinter";

    private static final long LOG_FILE_MAX_LENGTH = 1 << 20;

    private final File mLogDir;

    public LogFilePrinter(File dir) {
        mLogDir = dir;
        if (!mLogDir.exists() && !mLogDir.mkdirs()) {
            Log.e(TAG, "fail to create dir: " + mLogDir);
        }
    }

    @Override
    public void log(int priority, String tag, String msg) {
        final String finalMsg = String.format(Locale.ENGLISH, "%s %s/%s %s",
                new SimpleDateFormat("MM-dd HH:mm:ss.mmm", Locale.ENGLISH).format(System.currentTimeMillis()),
                getPriorityName(priority), tag, msg);
        LogFile.getInstance().writeLog(tag, finalMsg, mLogDir, LOG_FILE_MAX_LENGTH);
    }

    private String getPriorityName(int priority) {
        switch (priority) {
            case Log.ASSERT:
                return "A";
            case Log.DEBUG:
                return "D";
            case Log.ERROR:
                return "E";
            case Log.INFO:
                return "I";
            case Log.VERBOSE:
                return "V";
            default:
                return null;
        }
    }
}
